﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Tab2Sql.Forms;

namespace Tab2Sql.Classes
{
    /// <summary>
    ///
    /// </summary>
    public class LoadFile
    {
        long fileSize;
        bool largeFileMode;
        
        long lineCount;
        /// <summary>
        ///
        /// </summary>
        public long LineCount
        {
            get { return lineCount; }

        }
        /// <summary>
        ///
        /// </summary>
        public bool LargeFileMode
        {
            get { return largeFileMode; }
            set { largeFileMode = value; }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="inStr">
        /// A <see cref="inRtbox"/>
        /// </param>
        public LoadFile(string fileName, ref inRtbox inStr)
        {
         
            largeFileMode = false;
            if (!File.Exists(fileName))
            {
                inStr.Text += "File not found.";
            }
            else
            {
                lineCount = DGen.DFile.CountLinesInFile(fileName);
                FileInfo finfo = new FileInfo(fileName);
                fileSize = finfo.Length;
                if (fileSize>Properties.Settings.Default.LargeFileMode)
                {
                    largeFileMode = true;
                    readSample(fileName, ref inStr, 100000);
                    
                }
                else
                {
                    inStr.DoLoadFile(fileName);
                    Tab2Sql.Classes.db.historyDB History = new Tab2Sql.Classes.db.historyDB();
                    History.AddFileEntry(fileName);
                    History = null;
                }
            }
        }
        private void readSample(string fileName, ref inRtbox inStr, int p)
        {
            StringBuilder sampleText = new StringBuilder(72000);
            using (System.IO.StreamReader file = new System.IO.StreamReader(fileName))
            {
                for (int i = 0; i < p; i++)
                {
                    if (file.ReadLine() != null)
                    {
                        sampleText.AppendLine(file.ReadLine());
                    }
                }
                
            }
            inStr.Text = sampleText.ToString();
        }



    }
}
/*
        private void doCopy(FileStream fs1, FileStream fs2)
        {
            int i;
            do
            {
                i = fs1.ReadByte();
                if (i != -1) fs2.WriteByte((byte)i);
            } while (i != -1);
        }
        private int doCopy(string path, string tempPath)
        {
            int result = -1;
            try
            {
                File.Copy(path, tempPath, true);
                result = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                result = 1;
            }
            return result;
        }


        public int DoLoadFile(string path)
        {

            FileInfo f = new FileInfo(path);
            if (f.Length > 20000000)
            {
                SplitFile(path);
            }

            s.Start();
            string tPath = DGen.DFile.uniqueFileName();

            if (doCopy(path, tPath) == 0)
            {
                using (FileStream fileStream = new FileStream(tPath, FileMode.Open, FileAccess.ReadWrite))
                {
                    fileStream.SetLength(Math.Min(10000000, fileStream.Length));
                    this.LoadFile(fileStream, RichTextBoxStreamType.PlainText);
                }
                IsDone = true;
                ImportedFileName = tPath;
            }
            s.Stop();
            return 0;

        }

        

        

        public int SplitFile(string fName)
        {
            string firstLine = DGen.DFile.GetLineAt(fName, 0);
            string guidFileName = DGen.DFile.uniqueFileName();
            File.WriteAllText(guidFileName, firstLine);


            int index = 0;
            long maxFileSize = 65536 * 256;
            byte[] buffer = new byte[65536];
            
            string tPath =Path.GetDirectoryName(fName);
            using (Stream source = File.OpenRead(fName))
            {
                while (source.Position < source.Length)
                {
                    index++;

                    // Create a new sub File, and read into t
                    string newFileName = Path.Combine(tPath, Path.GetFileNameWithoutExtension(fName));
                    newFileName += index.ToString() + Path.GetExtension(fName);
                    
                    using (Stream destination = File.OpenWrite(newFileName))
                    {
                        while (destination.Position < maxFileSize)
                        {
                            // Work out how many bytes to read
                            int bytes = source.Read(buffer, 0, (int)Math.Min(maxFileSize, buffer.Length));
                            destination.Write(buffer, 0, bytes);

                            // Are we at the end of the file?
                            if (bytes < Math.Min(maxFileSize, buffer.Length))
                            {
                                break;
                            }
                        }
                    }
                    DGen.DFile.PutLineAt(newFileName, 0, firstLine);

                }
            }

            return 0;
        }*/